package com.xbdun.web.bmp.controller.system;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.comweb.bmp.frame.annotation.XbdunLogin;
import com.xbdun.comweb.bmp.util.UserInfoUtil;
import com.xbdun.facade.bmp.constant.DubboBmpConst;
import com.xbdun.facade.bmp.request.system.SysOrgFiledsRequest;
import com.xbdun.facade.bmp.request.system.SysOrgModifyRequest;
import com.xbdun.facade.bmp.result.system.SysOrgFiledsResult;
import com.xbdun.facade.bmp.result.system.SysOrgResult;
import com.xbdun.facade.bmp.service.system.SysOrgFacade;
import com.xbdun.facade.bmp.service.system.SysOrgFiledsFacade;
import com.xbdun.facade.validatebmp.constant.DubboValidateBmpConst;
import com.xbdun.facade.validatebmp.menus.UploadFileFormat;
import com.xbdun.facade.validatebmp.request.OpenUploadReqeust;
import com.xbdun.facade.validatebmp.service.upload.OpenUploadFacade;

import lombok.extern.slf4j.Slf4j;

/**
 * 企业信息
 * @ClassName: S100101104Controller
 * @Description: 企业信息
 * @category 企业信息
 * @author wwq@xbdun.com
 * @date 2017年6月13日 上午11:37:27
 */
@Controller
@Slf4j
@XbdunLogin()
public class S100101104Controller extends BaseSystemController{
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysOrgFacade sysOrgFacade;
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysOrgFiledsFacade sysOrgFiledsFacade;
	@Reference(registry=DubboValidateBmpConst.REGISTRYS)
	OpenUploadFacade openUploadFacade;
	/**
	 * 企业信息
	 * @return
	 */
	@RequestMapping("100101104")
    public String index(){
        return ROOT_PATH+"100101104";
    }
	
	@RequestMapping("100101104X")
	@ResponseBody
	public SysOrgResult getSysOrgByOrgCode(){
		SysOrgResult result = new SysOrgResult();
		try {
			//获取企业信息
			result = sysOrgFacade.getSysOrgByOrgCode(UserInfoUtil.getClient(getRequest()).getOrgCode());
		} catch (Exception e) {
			log.error("获取企业信息失败：{}"+e);
		}
		return result;
	}
	@RequestMapping("100101104XX")
	@ResponseBody
	public XbdUnStringResult getLogoAndRecordImg(String logo,String recordImg){
		XbdUnStringResult result = new XbdUnStringResult();
		OpenUploadReqeust request = null;
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			if(StringUtils.isNotBlank(logo)){
				request = new OpenUploadReqeust();
				request.setFiledId(new long[]{Long.parseLong(logo)});
				request.setOrgCode(UserInfoUtil.getClient(getRequest()).getOrgCode());
				request.setUploadFormat(UploadFileFormat.DEFAULT.name());
				data.put("logo", openUploadFacade.getUrl(request).getData());
			}
			if(StringUtils.isNotBlank(recordImg)){
				request = new OpenUploadReqeust();
				request.setFiledId(new long[]{Long.parseLong(recordImg)});
				request.setOrgCode(UserInfoUtil.getClient(getRequest()).getOrgCode());
				request.setUploadFormat(UploadFileFormat.DEFAULT.name());
				data.put("recordImg", openUploadFacade.getUrl(request).getData());
			}
			result.setData(JSON.toJSONString(data));
		} catch (Exception e) {
			log.error("获取图片异常：{}"+e);
			result.setBizException(e);
		}
		return result;
	}
	/**
	 * @Title getOrgFileds
	 * @Description 获取企业附属属性
	 * @param recordImg
	 * @return SysOrgFiledsResult 返回值类型
	 */
	@RequestMapping("100101104XXX")
	@ResponseBody
	public SysOrgFiledsResult getSysOrgFiledsByOrgCode(){
		SysOrgFiledsResult result = new SysOrgFiledsResult();
		try {
			result = sysOrgFiledsFacade.getSysOrgFiledsByOrgCode(UserInfoUtil.getClient(getRequest()));
		} catch (Exception e) {
			log.error("result异常：{}"+e);
			result.setBizException(e);
		}finally{
			log.info("获取机构附属信息结果：{}"+result);
		}
		return result;
	}
	
	/**
	 * 修改机构信息
	 * @Title updateSysOrg
	 * @Description 修改机构信息
	 * @return XbdUnStringResult 返回值类型
	 */
	@RequestMapping("100101104100")
	@ResponseBody
	public XbdUnStringResult updateSysOrg(SysOrgModifyRequest request){
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("修改机构信息请求参数：{}"+ request);
			if(request==null){
				throw BizException.PARAM_CHKE_FAIL.newInstance("请求参数不能为空");
			}
			if(request.getId()==null||request.getId()<=0){
				throw BizException.PARAM_CHKE_FAIL.newInstance("机构id不能为空");
			}
			sysOrgFacade.modifySysOrg(request, UserInfoUtil.getClient(getRequest()));
			//删除缓存图片
		} catch (Exception e) {
			log.error("修改机构信息异常：{}"+e);
			result.setBizException(e);
		}
		return result;
	}
	
	/**
	 * @Title updateSysOrg
	 * @Description 修改机构附属信息
	 * @return XbdUnStringResult 返回值类型
	 */
	@RequestMapping("100101104100X")
	@ResponseBody
	public XbdUnStringResult updateSysOrg(SysOrgFiledsRequest request){
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("修改机构附属信息请求参数：{}"+ request);
			sysOrgFiledsFacade.modifySysOrgFileds(request, UserInfoUtil.getClient(getRequest()));
		} catch (Exception e) {
			log.error("修改机构附属信息异常：{}"+e);
			result.setBizException(e);
		}
		return result;
	}
}
